package mavenProject.DAO;

import java.util.ArrayList;
import java.util.List;

import mavenProject.Model.Answer;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class AnswerDAOImpl extends HibernateDaoSupport implements AnswerDAO{

private HibernateTemplate hibernateTemplate;
	
	@Autowired
	public void init(SessionFactory factory) {
	    setSessionFactory(factory);
	}
//	public void setSessionFactory(SessionFactory sessionFactory) {
//		hibernateTemplate = new HibernateTemplate(sessionFactory);
//	}
	
	public Answer getAnswer(int id) {
		Criteria criteria = getSession().createCriteria(Answer.class);		
		criteria.add(Restrictions.eq("id", id));
		return (Answer) criteria.uniqueResult();
		
		
		//return (Answer) hibernateTemplate.find("from Answer where id = "+id);
	
	}
	

	@SuppressWarnings("unchecked")
	public ArrayList<Answer> getAllAnswers() {
		
		Criteria criteria = getSession().createCriteria(Answer.class);		
		return (ArrayList<Answer>) criteria.list();
		
		//return  (ArrayList<Answer>) hibernateTemplate.find("from Answer");
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Answer> getAnswersByQuestionId(Integer id){
		
		Criteria criteria = getSession().createCriteria(Answer.class);		
		criteria.add(Restrictions.eq("question", id));
		return criteria.list();
		
		//		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Answer.class);
		//		criteria.add(Restrictions.eq("question", id));
		//		return criteria.list();
				
		//return  (ArrayList<Answer>) hibernateTemplate.find("from Answer where questionID = "+ id);
	
		
		
	}
}
